# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import unittest, time

class Login(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "http://www.baidu.com"
        self.verificationErrors = []
    
    def test_Login(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        time.sleep(3)
        '''点击登录'''
        driver.find_element_by_xpath("//a[@id='lb']").click()
        driver.find_element_by_xpath("//div[contains(concat(' ', @class, ' '), ' bd_dialog_handle ')]")
        print driver
        '''切换到登陆浮层'''
        driver.switch_to_frame("login_iframe")
        time.sleep(5)
        driver.find_element_by_xpath("//input[@id='pass_login_username_0']").clear()
        driver.find_element_by_xpath("//input[@id='pass_login_username_0']").send_keys("zhengchunxi@foxmail.com")
        driver.find_element_by_xpath("//input[@id='pass_login_password_0']").clear()
        driver.find_element_by_xpath("//input[@id='pass_login_password_0']").send_keys("testMiaozhen")
        time.sleep(3)
        '''取消记住密码'''
        driver.find_element_by_xpath("//input[@id='pass_login_rem_0']").click()
        time.sleep(3)
        '''点击【登陆】按钮'''
        driver.find_element_by_xpath("//input[@id='pass_login_input_submit_0']").click()
    
    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException : return False
        return True
    
    def tearDown(self):
        time.sleep(5)
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()

